<template>
  <div class="photoinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :swipeList="posterList" :isfull="false" />
        </div>
      </div>
    </div>
    <!-- 购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：<del>{{goodsInfo.market_price}}</del>&nbsp;&nbsp;销售价：<span class="now_price">{{goodsInfo.sell_price}}</span>
          </p>
          <p>购买数量：<numbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity" /></p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfo.goods_no}}</p>
          <p>库存情况：{{goodsInfo.stock_quantity}}</p>
          <p>生产时间：{{goodsInfo.add_time | dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import numbox from '@/components/subcomponents/goodsinfo_numbox'
import swiper from '@/components/subcomponents/swiper'
import {Toast} from 'mint-ui'
export default {
  data () {
    return {
      id: this.$route.params.id,
      posterList: [],
      goodsInfo: [],
      ballFlag: false,
      // 默认加购数量为1
      selectedCount: 1
    }
  },
  created () {
    this.getPoster()
    this.getGoodsInfo()
  },
  methods: {
    getPoster () {
      this.$http.get('http://www.liulongbin.top:3005/api/getthumimages/' + this.id).then(res => {
        if (res.body.status === 0) {
          res.body.message.forEach(item => {
            item.img = item.src
          })
          this.posterList = res.body.message
        } else {
          Toast('加载图片失败...')
        }
      })
    },
    getGoodsInfo () {
      this.$http.get('http://www.liulongbin.top:3005/api/goods/getinfo/' + this.id).then(res => {
        if (res.body.status === 0) {
          this.goodsInfo = res.body.message[0]
        } else {
          Toast('加载详情失败...')
        }
      })
    },
    goDesc (id) {
      // 跳转到详情
      this.$router.push({name: 'goodsdesc', params: {id}})
    },
    goComment (id) {
      // 跳转到评论
      this.$router.push({name: 'goodscomment', params: {id}})
    },
    addToShopCar () {
      // 添加到购物车
      this.ballFlag = !this.ballFlag
    },
    beforeEnter (el) {
      el.style.transform = 'translate(0, 0)'
    },
    enter (el, done) {
      // 获取小球所在的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect()
      // 获取徽标所在的位置
      const badgePosition = document.getElementById('badge').getBoundingClientRect()
      const xDist = badgePosition.left - ballPosition.left
      const yDist = badgePosition.top - ballPosition.top

      el.style.transform = `translate(${xDist}px, ${yDist}px)`
      el.style.transition = 'all 0.6s cubic-bezier(.4, -0.3, 1, .68)'
      done()
    },
    afterEnter (el) {
      this.ballFlag = !this.ballFlag
    },
    getSelectedCount (count) {
      this.selectedCount = count
    }
  },
  components: {
    swiper,
    numbox
  }
}
</script>

<style lang="scss" scoped>
.photoinfo-container{
  background-color: #f9f9f9;
  overflow: hidden;
  .now_price{
    font-size: 16px;
    color: red;
    font-weight: bold;
  }
  .mui-card-footer{
    display: block;
    button{
      margin: 15px 0;
    }
  }
  .ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 391px;
    left: 148px;
  }
}
</style>
